const express = require('express')
const router = express.Router()
const jwt = require('jsonwebtoken');
const secert = 'denglu';


//导入数据库 sqlFn('sql',[],res=>{})
const sqlFn = require('../mysql')

router.post('/login', (req, res) => {
    console.log('query:', req.query)
    let { username, password } = req.query;
    if (username === 'admin') {
        if (password === '123456') {
            res.send({
                code: 1,
                username: 'admin',
                token: jwt.sign({ username: 'admin' }, secert, { expiresIn: 20 }),
                message: '登录成功'
            })
        } else {
            res.send({
                code: 2,
                message: '登录失败，密码错误！'
            })
        }
    } else {
        res.send({
            code: 3,
            message: '登录失败，请检查用户名！'
        })
    }
})




router.get('/getUsers', (req, res) => {
    const sqlLen = "select * from users";
    sqlFn(sqlLen, null, data => {
        let len = data.length;
        if (len > 0) {
            res.send({
                status: 200,
                data: data
            })
        } else {
            res.send({
                status: 500,
                msg: "暂无数据"
            })
        }
    })
})


/**
 * 商品查询接口 search
 * 参数：search  {search:xx}
 */
router.get("/users_search", (req, res) => {
    console.log('query:', req.query)
    console.log('传过来的参数:', req.query.search_word)
    var search = req.query.search_word;
    const sql = "select * from users where concat(`username`,`phone`,`email`,`address`) like '%" + search + "%'";
    sqlFn(sql, null, (result) => {
        console.log('result:', result)
        console.log('result长度:', result.length)
        if (result.length > 0) {
            console.log("我在这")
            res.send({
                status: 200,
                result: result
            })
        } else {
            res.send({
                status: 500,
                result: result,
                msg: "暂无数据"
            })
        }
    })
})




router.post('/users_submit', (req, res) => {
    console.log('req:', req.query);
    const sql = "insert into users(username,phone,email,address,birth,create_time) values(?,?,?,?,?,?)";
    let arr = [req.query.username, req.query.phone, req.query.email, req.query.address, req.query.birth.slice(0, 10), new Date()]
    sqlFn(sql, arr, (result) => {
        console.log('result:', result)
        res.send({
            res_code: '发送成功',
        });
    })
});


router.post('/users_editor', (req, res) => {
    console.log('req:', req.query);
    console.log('birth:', req.query.birth.slice(0, 10))
    const sql = "update users set username=?,phone=?,email=?,address=?,birth=?,editor_time=? where id=?";
    let arr = [req.query.username, req.query.phone, req.query.email, req.query.address, req.query.birth.slice(0, 10), new Date(), Number(req.query.id)]
    sqlFn(sql, arr, (result) => {
        console.log('result:', result)
        res.send({
            res_code: '修改成功',
        });
    })
});

router.post('/users_delete', (req, res) => {
    console.log('req:', req.query);
    const sql = "delete from users where id=?";
    let arr = [Number(req.query.users_id)]
    sqlFn(sql, arr, (result) => {
        console.log('result:', result)
        res.send({
            res_code: '删除成功',
        });
    })
});
router.post('/users_deleteSelect', (req, res) => {
    console.log(req.query.data)
    let data = JSON.parse(req.query.data);
    const sql = "delete from users where id=?";

    data.forEach(el => {
        let users_id = [Number(el.id)]
        sqlFn(sql, users_id, (result) => {
            console.log('result:', result)
        })
    });
    res.send({
        res_code: '删除成功',
    });

});

module.exports = router